<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.mapper.${className}Mapper">

    <resultMap id="result" type="${packageName}.domain.${className}">
        <#list columns as column>
            <#if column.isPrimaryKey == true>
                <id column="${column.name}" property="${column.property}" jdbcType="${column.jdbcType}"/>
            <#else>
                <result column="${column.name}" property="${column.property}" jdbcType="${column.jdbcType}"/>
            </#if>
        </#list>
    </resultMap>

    <sql id="table">
        <#--如果数据库类型为ORACLE需要加上表空间-->
        <#if dbType == "oracle">
            <#if dbSchema?has_content>
                ${dbSchema}.${tableName}
            <#else>
                ${tableName}
            </#if>
        <#else>
            ${tableName}
        </#if>
    </sql>

    <sql id="columns">
        <#list columns as column>
            ${column.name}<#if column_has_next>,</#if>
        </#list>
    </sql>

    <sql id="dynamicWhere">
        <where>
            <#list columns as column>
                <#-- 字符串类型的， 只要判空，判断空字符串，其他的类型判空就行-->
                <#if column.jdbcType == "VARCHAR">
                    <if test="${column.property} != null and ${column.property} != ''">
                        AND ${column.name} = ${r'#{'}${column.property}${r'}'}
                    </if>
                <#else>
                    <if test="${column.property} != null">
                        AND ${column.name} = ${r'#{'}${column.property}${r'}'}
                    </if>
                </#if>
            </#list>
        </where>
    </sql>

    <sql id="sql1">
        <#list columns as column>
        <#-- 字符串类型的， 只要判空，判断空字符串，其他的类型判空就行-->
            <#if column.jdbcType == "VARCHAR">
                <if test="${column.property} != null and ${column.property} != ''">
                    ${column.name},
                </if>
            <#else>
                <if test="${column.property} != null">
                    ${column.name},
                </if>
            </#if>
        </#list>
    </sql>

    <sql id="sql2">
        <#list columns as column>
        <#-- 字符串类型的， 只要判空，判断空字符串，其他的类型判空就行-->
            <#if column.jdbcType == "VARCHAR">
                <if test="${column.property} != null and ${column.property} != ''">
                    ${r'#{'}${column.property}${r'}'},
                </if>
            <#else>
                <if test="${column.property} != null">
                    ${r'#{'}${column.property}${r'}'},
                </if>
            </#if>
        </#list>
    </sql>

    <sql id="sql3">
        <#list columns as column>
            <#if column.jdbcType == "VARCHAR">
                <if test="${column.property} != null and ${column.property} != ''">
                    ${column.name} = ${r'#{'}${column.property}${r'}'},
                </if>
            <#else>
                <if test="${column.property} != null">
                    ${column.name} = ${r'#{'}${column.property}${r'}'},
                </if>
            </#if>
        </#list>
    </sql>

    <sql id="sql4">
        <#list columns as column>
            ${r'#{item.'}${column.property},jdbcType=${column.jdbcType}${r'}'},
        </#list>
    </sql>

    <#--<sql id="sql5">-->
    <#--    <#list columns as column>-->
    <#--        ${column.name}=(${column.name}),-->
    <#--    </#list>-->
    <#--</sql>-->

    <insert id="add" parameterType="${packageName}.domain.${className}">
        insert into
        <include refid="table"/>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="sql1"></include>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="sql2"></include>
        </trim>
    </insert>

    <update id="updateById" parameterType="${packageName}.domain.${className}">
        update
        <include refid="table"/>
        <set>
            <include refid="sql3"></include>
        </set>
        WHERE ${idColum} = ${r'#{'}${idProperty}${r'}'}
    </update>

    <delete id="deleteById">
        <#--转译 ${r'文本内容'}-->
        DELETE FROM
        <include refid="table"/>
        WHERE ${idColum} = ${r'#{'}${idProperty}${r'}'}
    </delete>

    <delete id="deleteBatchIds">
        DELETE FROM
        <include refid="table"/>
        WHERE ${idColum} in
        <foreach collection="pkList" item="i" open="(" separator="," close=")">
            ${r'#{'}i${r'}'}
        </foreach>
    </delete>

    <select id="findOneById" resultMap="result">
        SELECT
        <include refid="columns"/>
        FROM
        <include refid="table"/>
        WHERE ${idColum} = ${r'#{'}${idProperty}${r'}'}
    </select>

    <select id="findAll" parameterType="${packageName}.domain.${className}" resultMap="result">
        SELECT
        <include refid="columns"/>
        FROM
        <include refid="table"/>
    </select>

    <select id="find" parameterType="${packageName}.domain.${className}" resultMap="result">
        SELECT
        <include refid="columns"/>
        FROM
        <include refid="table"/>
        <include refid="dynamicWhere"/>
    </select>

    <select id="count" parameterType="${packageName}.domain.${className}" resultType="java.lang.Long">
        SELECT count(*)
        FROM
        <include refid="table"/>
        <include refid="dynamicWhere"/>
    </select>

<#--如果数据库类型为ORACLE需要加上表空间-->
<#if dbType == "oracle">
    <insert id="batchInsert" >
        insert into
        <include refid="table"/>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="columns"></include>
        </trim>
        select * from(
        <foreach collection="list" item="item" index="index" separator="UNION ALL">
            select
            <trim suffixOverrides=",">
                <include refid="sql4"></include>
            </trim>
            FROM dual
        </foreach>
        )
    </insert>
<#else>
    <insert id="batchInsert" >
        insert into
        <include refid="table"/>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="columns"></include>
        </trim>
        values
        <foreach collection="list" item="item" index="index"
                 separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <include refid="sql4"></include>
            </trim>
        </foreach>
    </insert>
</#if>

<#--&lt;#&ndash;如果数据库类型为ORACLE需要加上表空间&ndash;&gt;-->
<#--<#if dbType == "oracle">-->
<#--    <update id="batchUpsert" >-->
<#--        &lt;#&ndash;MERGE INTO employees e&ndash;&gt;-->
<#--        &lt;#&ndash;USING (SELECT 1 AS employee_id, 'John Doe' AS employee_name, 5000 AS salary FROM DUAL) new_data&ndash;&gt;-->
<#--        &lt;#&ndash;ON (e.employee_id = new_data.employee_id)&ndash;&gt;-->
<#--        &lt;#&ndash;WHEN MATCHED THEN&ndash;&gt;-->
<#--        &lt;#&ndash;UPDATE SET e.employee_name = new_data.employee_name, e.salary = new_data.salary&ndash;&gt;-->
<#--        &lt;#&ndash;WHEN NOT MATCHED THEN&ndash;&gt;-->
<#--        &lt;#&ndash;INSERT (employee_id, employee_name, salary) VALUES (new_data.employee_id, new_data.employee_name, new_data.salary);&ndash;&gt;-->
<#--    </update>-->
<#--<#else>-->
<#--    <update id="batchUpsert" >-->
<#--        insert into-->
<#--        <include refid="table"/>-->
<#--        <trim prefix="(" suffix=")" suffixOverrides=",">-->
<#--            <include refid="columns"></include>-->
<#--        </trim>-->
<#--        values-->
<#--        <foreach collection="list" item="item" index="index"-->
<#--                 separator=",">-->
<#--            <trim prefix="(" suffix=")" suffixOverrides=",">-->
<#--                <include refid="sql4"></include>-->
<#--            </trim>-->
<#--        </foreach>-->
<#--        on duplicate key update-->
<#--        <trim suffixOverrides=",">-->
<#--            <include refid="sql5"></include>-->
<#--        </trim>-->
<#--    </update>-->
<#--</#if>-->
</mapper>
